from flask import Blueprint, request, jsonify
from api.user_api import User_reg, User_login, User_find
from utils.secuirtyCode import SecuirtyCode as SC
from utils.data_proccess import image_To_Str as its
import datetime

user = Blueprint('user', __name__)


# 用户模块 user/list  user/login   user/register  user/findloss
# 路由配置

# @user.route('/list')
# def user_list():
#      #使用户列表功能
#      list = User_List()
#      #{”code“:0,data:list}
#      return jsonify(list)


# 前端登陆界面传递的表单格式如下：
# form: {
#           username: '',
#           pwd: '',
#           checked: false,
#         }
# 登录 POST username password
@user.route('/login', methods=['POST'])
def login():
    username = request.form["username"]
    password = request.form["pwd"]
    result = User_login(username, password)
    return jsonify(result)
    # return jsonify("0k")


# 注册方法的表单数据：
# form: {
#           username: '',
#           pwd: '',
#           checkpwd: '',
#           checked: false,
#         }
#  POST 注册 username password phone others
@user.route('/register', methods=['POST'])
def reg():
    # GET path 参数列表
    username = request.form["username"]
    password = request.form["pwd"]
    # 验证
    # 使用户注册功能
    result = User_reg({
        "userName": username,
        "userPWD": password,
        "userInfo": 'init',
        "userEditTime": datetime.date.today()
    })
    return jsonify(result)

# 注册方法的表单数据：
# form: {
#           username: '',
#           confirm: '',
#           checkpwd: '',
#           newpwd: '',
#           info:''
#         },


@user.route('/findloss', methods=['POST'])
def findloss():
    # post path 参数列表
    username = request.form["username"]
    newpwd = request.form["newpwd"]+' NC'
    userInfo = request.form["info"]+request.form["confirm"]
    # 找回功能
    result = User_find(username, newpwd, userInfo)
    return jsonify(result)


@user.route('/securitycode', methods=['get'])
def securitycode():
    # GET path 参数列表
    # try:
    sc = SC()
    sc.drawPic()
    num = sc.save()
    image = its("pic\pic.png")
    data = {
        "confirm": "success",
        "img": image,
        'img_num': num
    }
    return jsonify(data)
    # except:
    #     return jsonify({"confirm": "fail"})

